package com.itheima.web.utils;

import com.itheima.domain.system.SysLog;
import com.itheima.domain.system.User;
import com.itheima.service.system.ISysLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
public class LogAspect {

    @Autowired
    private HttpServletRequest request;//得到用户登录信息从request.getSession域中获取

    @Autowired
    private ISysLogService sysLogService;


    @Around("execution(* com.itheima.web.controller.*.*.*(..))")
    public Object aroundAdvice(ProceedingJoinPoint pjp){

        //获取当前登录的用户
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        //判断用户是否登录
        if(user!=null){

            //从pjp中获取方法签名
            Signature signature = pjp.getSignature();
            //判断是不是方法签名
            if(signature instanceof MethodSignature){
                //强转
                MethodSignature methodSignature =(MethodSignature) signature;

                //获取当前执行方法
                Method method = methodSignature.getMethod();
                //判断当前方法上是否有requestMapping注解
                boolean hasAnn = method.isAnnotationPresent(RequestMapping.class);
                if(hasAnn){
                    //取出注解
                    RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                    //得到注解的属性，用于填充syslog对象
//                    String[] value = requestMapping.value();
//                    StringBuilder stringBuilder = new StringBuilder();
//                    if(value.length>1){
//                        for (String s : value) {
//                            stringBuilder.append(s).append(",");
//                        }
//                    }else {
//                        stringBuilder.append(value);
//                    }
                    String name = requestMapping.name();
                    //创建日志对象
                    SysLog sysLog = new SysLog();
                    sysLog.setIp(request.getRemoteAddr());//获取ip
                    sysLog.setTime(new Date());
                    sysLog.setMethod(method.getName());
                    sysLog.setAction(name);//获取当前执行方法的说明
                    sysLog.setUserName(user.getUserName());
                    sysLog.setCompanyId(user.getCompanyId());
                    sysLog.setCompanyName(user.getCompanyName());

                    //保存日志
                    sysLogService.save(sysLog);

                }
            }
        }

        //获取执行方法所需的参数
        Object[] args = pjp.getArgs();
        //定义返回值
        Object rtValue = null;

        try {
            //切入点方法的执行
            rtValue = pjp.proceed(args);
        } catch (Throwable t) {
            t.printStackTrace();
        }

        return rtValue;
    }



}
